
package com.monlyu.config;

import java.util.List;

import org.dom4j.Element;

import com.monlyu.bean.AuthCodeBean;
import com.monlyu.bean.DataBaseBean;
import com.monlyu.util.StringUtil;

/**
 * 一些应用程序发布时的一些参数
 * 
 * @author monlyu
 */
public class AppArgs {

    // 剩余天数
    public static int DAY_LEFT = 30;
    /** oracle */
    public static final String DB_ORACLE = "ORACLE";
    /** my sql */
    public static final String DB_MYSQL = "MYSQL";
    /** SQL server */
    public static final String DB_SQLSERVER = "SQLSERVER";

    /** 默认实体类的包名 */
    public static final String VO_PACKAGE = "com.monlyu.vo";

    /** 加载基础数据的时候,默认的基础数据文件夹 */
    public static final String DEFAULT_BASEDATA_DIR = "classpath:/baseData/";

    /** 程序的版本号 */
    private String version;
    /** 程序的目录路径 */
    private String webFileDir;

    /** 程序上传的附件保存的地方 */
    private String uploadDir;

    /** 富文本编辑器上传的图片保存路径 */
    private String keImgDir;

    /** 项目在网络上的相对路径 */
    private String webRelUrl;

    /** 验证码的一些参数 */
    private AuthCodeBean authCode;

    /** 存取相关数据库的一些信息 */
    private DataBaseBean dataBase;

    /** id每次添加的数量 */
    private long clusterNum;

    /** 是否为debug模式 */
    private boolean debug;

    public boolean isDebug(){
        return debug;
    }

    public void setDebug(boolean debug){
        this.debug = debug;
    }

    public DataBaseBean getDataBase(){
        return dataBase;
    }

    public void setDataBase(DataBaseBean dataBase){
        this.dataBase = dataBase;
    }

    public void setAuthCode(AuthCodeBean authCode){
        this.authCode = authCode;
    }

    public AuthCodeBean getAuthCode(){
        return authCode;
    }

    /**
     * 设置验证码的相关属性
     */
    public void setAuthCode(Element authCode){
        if (authCode != null) {
            AuthCodeBean bean = new AuthCodeBean();
            bean.setLength(authCode.attributeValue("length"));
            bean.setType(authCode.attributeValue("type"));
            bean.setIgnoreCase(authCode.attributeValue("ignoreCase"));
            bean.setNeedLines(authCode.attributeValue("needLines"));
            bean.setActive(authCode.attributeValue("active"));
            setAuthCode(bean);
        } else {
            setAuthCode(new AuthCodeBean());
        }
    }

    public long getClusterNum(){
        return clusterNum;
    }

    public void setClusterNum(String cluster){
        try {
            Long value = Long.parseLong(cluster.trim());
            this.clusterNum = value < 1 ? 1l : value;
        }
        catch (Exception e) {
            this.clusterNum = 1;
        }
    }

    /** 设置数据库相关的东西 */
    public void setDatabase(Element eleData){
        if (eleData != null) {
            DataBaseBean baseBean = new DataBaseBean();
            for (Element element : (List<Element>) eleData.elements()) {
                if (element.getName().equals("name")) {
                    baseBean.setName(element.getText());
                }
                if (element.getName().equals("version")) {
                    baseBean.setVersion(element.getText());
                }
                if (element.getName().equals("comment")) {
                    baseBean.setCommenOn(element.getText());
                }
                if (element.getName().equals("dbName")) {
                    baseBean.setDbName(element.getText());
                }
                if (element.getName().equals("encoding")) {
                    baseBean.setEncoding(element.getText());
                }
            }
            setDataBase(baseBean);
        } else {
            setDataBase(new DataBaseBean(DB_MYSQL, "5.5", false));
        }
    }

    public String getVersion(){
        return version;
    }

    public void setVersion(String version){
        this.version = version;
    }

    /** 获取项目在网络上的相对路径 */
    public String getWebRelUrl(){
        return webRelUrl;
    }

    public void setWebRelUrl(String webRelUrl){
        this.webRelUrl = StringUtil.trim(webRelUrl, "");
    }

    /** 获取文件的绝对路径 */
    public String getWebFileDir(){
        return webFileDir;
    }

    public void setWebFileDir(String webFileDir){
        this.webFileDir = webFileDir;
    }

    public String getUploadDir(){
        return uploadDir;
    }

    public void setUploadDir(String uploadDir){
        this.uploadDir = uploadDir;
    }

    public String getKeImgDir(){
        return keImgDir;
    }

    public void setKeImgDir(String keImgDir){
        this.keImgDir = keImgDir;
    }
}
